package com.jushu.storbox.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.huawei.sharedrive.sdk.android.modelV2.response.UserInfoV2;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

public class ShareFileSearchUserAdapter extends ArrayAdapter<UserInfoV2> {
	private List<UserInfoV2> adUserList;
	private Context context;
	private ViewHolder viewHolder;

	public ShareFileSearchUserAdapter(Context context, int resource, List<UserInfoV2> objects) {
		super(context, resource, objects);
		this.adUserList = objects;
		this.context = context;
	}

	@Override
	public int getCount() {
		return adUserList.size();
	}

	@Override
	public UserInfoV2 getItem(int position) {
		return adUserList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.simple_dropdown_item_layout, null);
			viewHolder = new ViewHolder();
			viewHolder.department = (TextView) convertView.findViewById(R.id.simple_department);
			viewHolder.name = (TextView) convertView.findViewById(R.id.simple_name);
			viewHolder.mail = (TextView) convertView.findViewById(R.id.mail);
			convertView.setTag(viewHolder);
		}
		viewHolder = (ViewHolder) convertView.getTag();
		UserInfoV2 userInfo = getItem(position);
		String description = userInfo.getDescription();
		String mail = userInfo.getEmail();
		String loginName = userInfo.getLoginName();
		
		if(StringUtil.isNotBlank(loginName)) {
			viewHolder.name.setVisibility(View.VISIBLE);
			viewHolder.name.setText(userInfo.getName()+"("+loginName+")");
		} else {
			viewHolder.name.setText(userInfo.getName());
		}
		
		if(StringUtil.isNotBlank(description)) {
			viewHolder.department.setVisibility(View.VISIBLE);
			viewHolder.department.setText(description);
		} else {
			viewHolder.department.setVisibility(View.GONE);
		}
		
		if(StringUtil.isNotBlank(mail)) {
			viewHolder.mail.setVisibility(View.VISIBLE);
			viewHolder.mail.setText(userInfo.getEmail());
		} else {
			viewHolder.mail.setVisibility(View.GONE);
		}
		return convertView;
	}

	public static class ViewHolder {
		public TextView name, jobNumber, department, mail;
	}
}
